import { getImageCategoryList, getImageList } from '@/api/image'
const state = {
  categoryList: [], // 分类类列表
  categoryTotalCount: null,
  imageList: {}, // 图片列表
  imageTotalCount: null,
  loading: false
}
// 修改状态
const mutations = {
  setCategoryList(state, list) {
    state.categoryList = list
  },
  setcategoryTotalCount(state, total) {
    state.categoryTotalCount = total
  },
  setImageList(state, list) {
    state.imageList = list
  },
  setImageTotalCount(state, total) {
    state.imageTotalCount = total
  },
  changeLoading(state, loading) {
    state.loading = loading
  }
}
// 执行异步
const actions = {
  // 获取分类列表
  async getCategoryList(context, data) {
    context.commit('changeLoading', true)
    try {
      const { list, totalCount } = await getImageCategoryList(data)
      context.commit('setCategoryList', list)
      context.commit('setcategoryTotalCount', totalCount)
    } catch (error) {
      console.log(error)
    } finally {
      context.commit('changeLoading', false)
    }
  },
  // 获取指定分类列表下图片列表
  async getImageList(context, data) {
    context.commit('changeLoading', true)
    try {
      const result = await getImageList(data)
      result.category_id = data.id
      const { category_id, list, totalCount } = result
      context.commit('setImageList', { list: list, id: category_id })
      context.commit('setImageTotalCount', totalCount)
    } catch (error) {
      console.log(error)
    } finally {
      context.commit('changeLoading', false)
    }
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
